package com.wbao.dianniu.im;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.data.NoticeQuestData;
import com.wbao.dianniu.listener.INoticeQuestListListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.ChatNotifyReadManager;
import com.wbao.dianniu.manager.NoticeQuestListManager;
import com.wbao.dianniu.ui.BaseActivity;
import com.wbao.dianniu.update.ChatUnreadChangeManger;
import com.wbao.dianniu.utils.ActivityUtils;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;

/**
 * 心情通知
 */
public class AmHelperActivity extends BaseActivity implements INoticeQuestListListener {

    private PullToRefreshListView pullListView;
    private ListView mListView;
    private NoticeQuestListManager manager;
    private MyAdapter adapter;
    private List<NoticeQuestData> mList;
    private final int PAGE_COUNTS = 10;
    private int currentPage = 0;
    private final int type = 2;
    private ChatNotifyReadManager chatNotifyReadManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_notice_qa_helper, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        init();
        requestData(currentPage);
        chatNotifyReadManager = new ChatNotifyReadManager(this);
        chatNotifyReadManager.chatNotifyRead(120);
        ChatUnreadChangeManger.getInstance().chatUnreadChange(120, 0);
        int unread = getIntent().getIntExtra(Const.BUNDLE_COUNT, 0);
        String title = getIntent().getStringExtra(Const.BUNDLE_POST);
        setTitleName(title == null ? "" : title);
        ChatUnreadChangeManger.getInstance().minusNotifyUnread(unread);
//        YWIMKit iMkit = LoginSampleHelper.getInstance().getIMKit();
//        IYWConversationService conversationService = iMkit.getConversationService();
//        YWConversation conversation = conversationService.getConversationByUserId("120", LoginSampleHelper.APP_KEY);
//        if (null != conversation)
//            conversationService.markReaded(conversation);
    }

    private void init() {
        pullListView = findViewById(R.id.notice_qa_listview);
        pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
        pullListView.setOnRefreshListener(onRefreshListener);
        mListView = pullListView.getRefreshableView();
        registerForContextMenu(mListView);

        adapter = new MyAdapter(this);
        mList = new ArrayList<>();
        adapter.addData(mList);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(itemClickListener);

    }

    OnRefreshListener onRefreshListener = new OnRefreshListener() {

        @Override
        public void onRefresh() {
            //  上拉刷新 加载请求数据
            pullUpRequest();
        }
    };

    OnItemClickListener itemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            // TODO Auto-generated method stub

        }
    };

    // 上拉刷新数据请求
    private void pullUpRequest() {
        //没有搜索结果 或者数据为空 上拉没有效果
        ++currentPage;
        requestData(currentPage);
    }

    private void requestData(int page) {
        manager = new NoticeQuestListManager(this);
        manager.addNoticeQuestListListener(this);
        manager.noticeQuestList(GlobalContext.getAccountId(), type, page * PAGE_COUNTS, PAGE_COUNTS);
    }

    @Override
    public void onNoticeQuestListFailure(int errorCode, String errorMsg) {
        // TODO Auto-generated method stub
        pullListView.onRefreshComplete(false, false);
    }

    @Override
    public void onNoticeQuestListSuccess(List<NoticeQuestData> list) {
        // TODO Auto-generated method stub
        pullListView.onRefreshComplete(false, false);
        if (null == list)
            return;
        adapter.addData(list);
        mList = adapter.getList();
        adapter.notifyDataSetChanged();
    }

    class MyAdapter extends BaseAdapter {

        private Context mContext;
        private List<NoticeQuestData> list;
        private LayoutInflater mInflater;

        public MyAdapter(Context context) {
            mContext = context;
            mInflater = LayoutInflater.from(mContext);
            if (null == list)
                list = new ArrayList<NoticeQuestData>();
        }

        public void addData(List<NoticeQuestData> addList) {
            for (int i = 0; i < addList.size(); i++) {
                list.add(addList.get(i));
            }
        }

        public List<NoticeQuestData> getList() {
            return list;
        }

        @Override
        public int getCount() {
            if (null == list)
                return 0;
            else
                return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.notice_qa_item, null);
                holder = new Holder();
                holder.head = convertView.findViewById(R.id.user_head);
                holder.name = convertView.findViewById(R.id.user_name);
                holder.label = convertView.findViewById(R.id.user_label);
                holder.addV = convertView.findViewById(R.id.add_v_iv);
                holder.date = convertView.findViewById(R.id.user_date);
                holder.answerContent = convertView.findViewById(R.id.answer_content);
                holder.qaContent = convertView.findViewById(R.id.answer_qa_content);
                holder.sexIV = convertView.findViewById(R.id.user_sex);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            NoticeQuestData data = list.get(position);
            if (data.getAnonymous() == 0) { // 实名
                holder.label.setVisibility(View.VISIBLE);
                holder.label.setText(data.getSendLabel() == null ? "" : data.getSendLabel());
                if (null == data.getSendSex()) {
                    Utils.showSex(mContext, null, holder.sexIV);
                } else {
                    Utils.showSex(mContext, data.getSendSex(), holder.sexIV);
                }
                if (data.isSendIsIndustryAuthority()) {
                    holder.addV.setVisibility(View.VISIBLE);
                } else {
                    holder.addV.setVisibility(View.GONE);
                }
            } else { // 匿名
                holder.label.setVisibility(View.GONE);
                holder.addV.setVisibility(View.GONE);
                holder.sexIV.setVisibility(View.GONE);
            }

            PictureDownloadUtils.displayHeadImage(mContext, holder.head, data.getSendHeadPic());
            holder.name.setText(data.getSendRealName() == null ? "" : data.getSendRealName());
            holder.date.setVisibility(View.VISIBLE);
            // 日期已经经过后台格式化
            holder.date.setText(data.getCreateDate() == null ? "" : data.getCreateDate());
            holder.answerContent.setText(data.getContent() == null ? "" : data.getContent());
            holder.qaContent.setText(data.getQuestContent() == null ? "" : "心情：" + data.getQuestContent());
            holder.answerContent.setOnClickListener(new MyClickListener(data));
            holder.qaContent.setOnClickListener(new MyClickListener(data));
            return convertView;
        }

        class Holder {
            public ImageView head;
            public TextView name;
            public TextView label;
            public ImageView addV;
            public TextView date;
            public TextView answerContent;
            public TextView qaContent;
            public ImageView sexIV;
            public TextView partnerTV;
        }

        private class MyClickListener implements OnClickListener {

            private NoticeQuestData data;

            public MyClickListener(NoticeQuestData data) {
                this.data = data;
            }

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.answer_content:
                    case R.id.answer_qa_content:
                        ActivityUtils.intoAmDetailActivity(mContext, data.getQuestId());
                        break;

                    default:
                        break;
                }
            }

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeNoticeQuestListListener(this);
        }
    }
}
